package com.gx.smart.building.ui.inspection.fragment

import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.KeyboardUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentInspectionBinding
import com.gx.smart.building.ui.inspection.adapter.PageAdapter
import com.gx.smart.building.ui.inspection.event.FinishedQuantityEvent
import com.gx.smart.building.ui.inspection.event.UnfinishedQuantityEvent
import com.gx.smart.building.ui.inspection.viewmodel.InspectionViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class InspectionFragment : BaseMVVMFragmentV2<InspectionViewModel>(),
    View.OnClickListener,
    TextView.OnEditorActionListener {
    private lateinit var viewBinding: FragmentInspectionBinding
    private lateinit var mPagerAdapter: PageAdapter

    override fun getToolbarTitle(): String = "巡检任务"

    override fun onBindLayout(): Int = R.layout.fragment_inspection

    override fun onBindViewModel(): Class<InspectionViewModel> = InspectionViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentInspectionBinding.inflate(layoutInflater, root, true)
        mPagerAdapter = PageAdapter(requireActivity())
        viewBinding.viewPager.adapter = mPagerAdapter
        viewBinding.viewPager.isUserInputEnabled = true
        viewBinding.search.setOnEditorActionListener(this)
        viewBinding.clear.setOnClickListener(this)
        viewBinding.viewPager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                val name = viewBinding.search.text.toString().trim()
                (mPagerAdapter.getItem(position) as CommonFragment).search(name)
            }
        })
    }

    override fun onStart() {
        super.onStart()
        val titles = resources.getStringArray(R.array.inspection)
        val mediator = TabLayoutMediator(
            viewBinding.tabLayout, viewBinding.viewPager
        ) { tab: TabLayout.Tab, position: Int ->
            tab.text = titles[position] + "10"
        }
        mediator.attach()
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            search()
            KeyboardUtils.hideSoftInput(v!!)
            return true
        }
        return false
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.clear -> viewBinding.search.editableText.clear()
        }
    }

    private fun search() {
        val name = viewBinding.search.text.toString().trim()
        (mPagerAdapter.getItem(viewBinding.viewPager.currentItem) as CommonFragment).search(name)
    }

    override fun initData() {
        registerEventBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleEventOne(event: FinishedQuantityEvent) {
        viewBinding.tabLayout.getTabAt(1)?.text = "已完成(${event.finishQuantity})"
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleEventTwo(event: UnfinishedQuantityEvent) {
        viewBinding.tabLayout.getTabAt(0)?.text = "未完成(${event.unfinishedQuantity})"
    }

}